package com.ivan.stu.notetool.utils;

import android.Manifest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;


import com.ivan.stu.notetool.activity.ShowImageActivity;

import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IVAN on 2019/2/28.
 */

public class LocalOpen {

    public static void openPicture(Context context, String path) {

        File file = new File(path);

        ArrayList<String> imageList = new ArrayList<>();

        imageList.add(path);
        ShowImageActivity.openImageActivity(context,0,imageList);

     /*   ((Activity)context).startActivityForResult(
                new Intent(context, IMGEditActivity.class)
                        .putExtra(IMGEditActivity.EXTRA_IMAGE_URI, file.getAbsolutePath())
                        .putExtra(IMGEditActivity.EXTRA_IMAGE_SAVE_PATH, file.getAbsolutePath()),
                2);*/


   /*     Intent intent = new Intent();
        Uri uri;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(context, "com.lancoo.studyassistant.littlestudyassistantv20.util.fileprovider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            uri = Uri.fromFile(file);
        }
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/*");
        if (!(context instanceof Activity)) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);*/
    }


    public static void openPicture(Context context, List<String> paths,int position) {


        ShowImageActivity.openImageActivity(context,position, (ArrayList<String>) paths);

     /*   ((Activity)context).startActivityForResult(
                new Intent(context, IMGEditActivity.class)
                        .putExtra(IMGEditActivity.EXTRA_IMAGE_URI, file.getAbsolutePath())
                        .putExtra(IMGEditActivity.EXTRA_IMAGE_SAVE_PATH, file.getAbsolutePath()),
                2);*/


   /*     Intent intent = new Intent();
        Uri uri;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(context, "com.lancoo.studyassistant.littlestudyassistantv20.util.fileprovider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            uri = Uri.fromFile(file);
        }
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/*");
        if (!(context instanceof Activity)) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);*/
    }

    public static  void  openCamera(Activity activity,File photo_file,String path) {
        //獲取系統版本
        int currentapiVersion = Build.VERSION.SDK_INT;

        Log.e("aaaaaaaaaa","当前系统版本");
        // 激活相机
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // 判断存储卡是否可以用，可用进行存储
        if (hasSdcard()) {
            if (!photo_file.exists()) {

                photo_file.mkdirs();
            }
            try {

                String temp = DateUtils.dateToStamp(DateUtils.getNowDate());
                photo_file = new File(path, "/"+temp+".jpg");
               // String photoPath = path + "/"+temp+".jpg";

                if (photo_file != null) {

                    Uri imageUri;
                    if (currentapiVersion < 24) {

                        Log.e("走的这","小于7.0");
                        // 从文件中创建uri
                        imageUri = Uri.fromFile(photo_file);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    } else {

                        Log.e("走的这","大于7.0");
                        //兼容android7.0 使用共享文件的形式
                        ContentValues contentValues = new ContentValues(1);
                        contentValues.put(MediaStore.Images.Media.DATA, photo_file.getAbsolutePath());
                        //检查是否有存储权限，以免崩溃
                        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                                != PackageManager.PERMISSION_GRANTED) {
                            //申请WRITE_EXTERNAL_STORAGE权限
                            Toast.makeText(activity,"请开启存储权限",Toast.LENGTH_SHORT).show();
                            return ;
                        }
                        imageUri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

                    }

                    // 开启一个带有返回值的Activity，请求码为PHOTO_REQUEST_CAREMA
                    activity.startActivityForResult(intent, 1);
                }

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else {

            Log.e("aaaaaaaaaaa","没有sd卡");
        }
    }

    public static boolean hasSdcard() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }



}
